package finalproject.model.rooms;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import finalproject.config.Randomizer;

/**
 * A class that build a map from a hardcoded room list
 * @author user
 *
 */
public class Mapper {

	/**
	 * The room list
	 */
    private List<Room> roomList;
    
    /** Map dimensions */
    private int mapWidth = 1, mapHeight = 3;
    
    /**
     * @param roomList the room list
     */
    public Mapper(List<Room> roomList)
    {
        this.roomList = roomList;
                
    }
    
    /**
     * Create the map
     * We shuffle the room list so it is a little bit more exciting
     * The room are still hard coded
     */
    public void createMap()
    {
        Collections.shuffle(roomList);
        
        Iterator<Room>i = roomList.iterator();
        int nb_exit = Randomizer.getRandom().nextInt(4)+1;  
        Room room = i.next(), neighbor;

        while(i.hasNext())
        {
            neighbor = i.next();
            setExit(room, nb_exit, neighbor);
            nb_exit--;
            if(nb_exit == 0)
            {
                nb_exit = Randomizer.getRandom().nextInt(3)+1;
                room = neighbor;
            }
        }
    }

    /**
     * Set the exit to a room depending on the number of exit
     * @param room The main room
     * @param nb_exit The number of exit we still have to put
     * @param neighbor the neighbor room
     */
    private void setExit(Room room, int nb_exit, Room neighbor) {
        switch(nb_exit)
        {
            case 2:
                room.setExit(Direction.NORTH, neighbor);
                neighbor.setExit(Direction.SOUTH, room);
                break;
                
            case 3 :
                room.setExit(Direction.SOUTH, neighbor);
                neighbor.setExit(Direction.NORTH, room);
                break;

            case 1: default:
                room.setExit(Direction.EAST, neighbor);
                neighbor.setExit(Direction.WEST, room);
                mapWidth++;
                break;
        }
    }

    /**
     * @returnthe starting room
     */
    public Room getStartingRoom()
    {
        return roomList.iterator().next();
    }
    
    public List<Room> getRoomList()
    {
		return roomList;    	
    }

	public int getMapWidth()
	{
		return mapWidth;
	}
	
	public int getMapHeight()
	{
		return mapHeight;
	}
 }
